


import 'dart:convert';
import 'dart:io';

import 'package:alip_app/Entities/CategoryResult.dart';
import 'package:alip_app/Entities/GetTopicResult.dart';
import 'package:alip_app/Entities/SignInResult.dart';
import 'package:alip_app/Entities/TopicDetailsResult.dart';
import 'package:alip_app/Entities/UILayoutResponse.dart';
import 'package:alip_app/StateManagement/ModelProvider.dart';
import 'package:dio/dio.dart';

abstract class SignInMode {
  Map<String, Object> getInformation();
}
class WithPassword extends SignInMode {
  final String userName;
  final String password;
  WithPassword({this.userName, this.password});
  @override
  Map<String, Object> getInformation() {
    return {
      "username": this.userName,
      "password": this.password,
    };
  }
}

class WithPhoneNumber extends SignInMode {
  final String mobile;
  final String code;
  WithPhoneNumber({this.code, this.mobile});
  @override
  Map<String, Object> getInformation() {
    return {
      "mobile": mobile,
      "code": code,
    };
  }
}

class HttpInterface extends Model {
  final String baseUrl;
  Dio _dio;
  HttpInterface({this.baseUrl}) {
    this._dio = Dio(BaseOptions(baseUrl: this.baseUrl));
  }
  void _throwIfHasErrorOccurred(Map<String, dynamic> responseData){
    if ((responseData["errcode"] ?? "") != "") throw responseData["errcode"];
  }
  Future<SignInResult> signIn(SignInMode mode) async {
    final result = await _dio.post("/mobcent/app/web/index.php?r=user/login", data: FormData.fromMap(mode.getInformation()));
    final map = json.decode(result.data);
    _throwIfHasErrorOccurred(map);
    return SignInResult.fromJson(map);
  }
  Future<GetTopicResult> getTopics({int page, int size, int boardId, String orderBy, String filterType}) async {
    orderBy ??= "new";
    filterType ??= "typeid";

    final result = await _dio.post("/mobcent/app/web/index.php?r=forum/topiclistex", options: Options(contentType: Headers.formUrlEncodedContentType), data: {
      "pageSize": size,
      "page": page,
      "orderby": orderBy,
      "sortby": orderBy,
      "boardId": boardId,
      "filtertype": filterType
    });
    final map = json.decode(result.data);
    _throwIfHasErrorOccurred(map);
    return GetTopicResult.fromJson(map);
  }
  Future<TopicDetailsResult> getTopicDetails({int topicId}) async {
    final formData = {
      "topicId": topicId
    };
    final result = await _dio.post("/mobcent/app/web/index.php?r=forum/postlist", data: formData, options: Options(contentType: Headers.formUrlEncodedContentType));
    final map = json.decode(result.data);
    _throwIfHasErrorOccurred(map);
    return TopicDetailsResult.fromJson(map);
  }

  Future<CategoryResult> getCategories() async {
    final result = await _dio.get("/mobcent/app/web/index.php?r=forum/forumlist");
    final map = json.decode(result.data);
    _throwIfHasErrorOccurred(map);
    return CategoryResult.fromJson(map);
  }

  Future<UILayoutResponse> getUILayouts() async {
    final result = await _dio.post("/mobcent/app/web/index.php?r=app/initui", data: {
      "egnVersion": "v2103.5"
    }, options: Options(contentType: Headers.formUrlEncodedContentType));
    final map = json.decode(result.data);
    _throwIfHasErrorOccurred(map);
    return UILayoutResponse.fromJson(map["body"]);
  }
}
